package com.fuyu.common.utils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.List;

@Data
public class PageHelperUtil {


    /**
     * 分页实体转换
     *
     * @param source 源类
     * @param list   目标类
     */
    public static <T, R> PageInfo<R> format(List<T> source, List<R> list) {
        PageInfo<T> of = PageInfo.of(source);
        PageInfo<R> myPageInfo = new PageInfo<>();

        myPageInfo.setTotal(of.getTotal());
        myPageInfo.setList(list);
        myPageInfo.setPageNum(of.getPageNum());
        myPageInfo.setPageSize(of.getPageSize());
        myPageInfo.setSize(of.getSize());
        myPageInfo.setStartRow(of.getStartRow());
        myPageInfo.setEndRow(of.getEndRow());
        myPageInfo.setPages(of.getPages());
        myPageInfo.setPrePage(of.getPrePage());
        myPageInfo.setNextPage(of.getNextPage());
        myPageInfo.setNavigatePages(of.getNavigatePages());
        myPageInfo.setNavigatepageNums(of.getNavigatepageNums());
        myPageInfo.setNavigateFirstPage(of.getNavigateFirstPage());
        myPageInfo.setNavigateLastPage(of.getNavigateLastPage());


        return myPageInfo;
    }

    public static <T, R> PageInfo<R> format(PageInfo<T> pageInfo, List<R> list) {
        PageInfo<R> myPageInfo = new PageInfo<>();
        myPageInfo.setTotal(pageInfo.getTotal());

        myPageInfo.setList(list);
        myPageInfo.setPageNum(pageInfo.getPageNum());
        myPageInfo.setPageSize(pageInfo.getPageSize());
        myPageInfo.setSize(pageInfo.getSize());
        myPageInfo.setStartRow(pageInfo.getStartRow());
        myPageInfo.setEndRow(pageInfo.getEndRow());
        myPageInfo.setPages(pageInfo.getPages());
        myPageInfo.setPrePage(pageInfo.getPrePage());
        myPageInfo.setNextPage(pageInfo.getNextPage());
        myPageInfo.setNavigatePages(pageInfo.getNavigatePages());
        myPageInfo.setNavigatepageNums(pageInfo.getNavigatepageNums());
        myPageInfo.setNavigateFirstPage(pageInfo.getNavigateFirstPage());
        myPageInfo.setNavigateLastPage(pageInfo.getNavigateLastPage());


        return myPageInfo;
    }

    public static void clearPage() {
        PageHelper.clearPage();
    }
}